﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.Mvc;

namespace FashionStoreMVC
{
    public static class FFormHelper
    {
        public static string ConvertPrice(double price)
        {
            string dPrice = price.ToString();

            int count = 3;
            int length = dPrice.Length;
            while (count < length)
            {
                dPrice = dPrice.Insert(length - count, ",");
                count += 3;
            }

            return dPrice + " VNĐ";
        }

        public static string ShortProductName(string productName)
        {
            if (productName.Length >= 30)
            {
                return productName.Substring(0, 30) + "...";
            }

            return productName;
        }

        public static string StandardDateTime(DateTime dateTime)
        {
            int day = dateTime.Day;
            int month = dateTime.Month;
            int year = dateTime.Year;

            TimeSpan a = DateTime.Now - dateTime;
            if (a.TotalDays > 365)
            {
                return string.Format("{0} Tháng {1},{2}", day, month, year); 
            }
            else if (a.TotalDays > 1)
            {
                return string.Format("{0} Tháng {1}", day, month);
            }
            else if (a.TotalDays == 1)
            {
                return "Hôm qua";
            }
            else
            {
                if (a.TotalHours >= 1)
                {
                    return string.Format("{0} giờ trước", a.TotalHours);
                }
                else if (a.TotalMinutes >= 1)
                {
                    return string.Format("{0} phút trước", a.TotalMinutes);
                }
                else
                {
                    return string.Format("{0} giây trước", a.TotalSeconds);
                }
            }
           
        }

        public static string StandardActionName(string actionName)
        {
            string r = "";

            for (int i = 0; i < actionName.Length; i++)
            {
                char c = actionName[i];
                if ('A' <= c && c <= 'Z')
                {
                
                    c = (char)((int)c + 32);

                    r += i == 0 ? c.ToString() : ("-" + c);
                }
                else {
                    r += c;
                }
            }

            return r;
        }

        public static int calculatePageTotal(int count, int perPage)
        {
            double d = count / (double) perPage;
            int i = (int)(count / perPage);

            return d > i ? i + 1 : i;
        }

        public static string GetPagingUrlTemplate(string url)
        {

            Regex r = new Regex("page=\\d");

            Match s = r.Match(url);

            if (string.IsNullOrEmpty(s.Value))
            {
                if (url.IndexOf("?") >= 0)
                {
                    url += "&page={0}";
                }
                else
                {
                    url += "?page={0}";
                }
                
            }
            else
            {
                url = url.Replace(s.Value, "page={0}");
            }
            return url;
        }

        public static string ReplaceHrefAttr(string html, string urlReplacement)
        {         
            Regex r = new Regex("href=\".*\"");
            string newHref = string.Format("href=\"{0}\"", urlReplacement);
            return r.Replace(html, newHref);
        }

        public static string AjaxActionLink(AjaxHelper ajax, string linkTitle, string actionName, string controller,
            object routeObject)
        {

            return System.Web.Mvc.Ajax.AjaxExtensions.ActionLink(ajax, linkTitle,
                                   FFormHelper.StandardActionName(actionName),
                                   controller,
                                  routeObject,
                                   new System.Web.Mvc.Ajax.AjaxOptions
                                   {
                                       HttpMethod = "POST",
                                       UpdateTargetId = "center-content",
                                       InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace,
                                       LoadingElementId = "loading-element",
                                       OnComplete = "hideLoading();",
                                       OnSuccess = "hideLoading();"
                                   }).ToString();
        }

        public static string ShortageProductDetail(string productDetail)
        {
            if (string.IsNullOrEmpty(productDetail))
            {
                return "Không có thông tin mô tả";
            }
            if (productDetail.Length >= 200)
            {
                return productDetail.Substring(0, 197) + "...";
            }
            
            return productDetail;
        }
    }
}